package game;

import java.awt.*;
import java.util.*;

public class FiveChess extends Game {
	private Map<Point, Integer> chessMap = new HashMap<Point, Integer>();

	@Override
	public void Init() {
		chessMap.clear();
		listener.OnPlayerColorChange(0, Color.black);
		listener.OnPlayerColorChange(1, Color.white);
	}

	@Override
	public Point GetBoardSize() {
		return new Point(13, 13);
	}

	@Override
	public void PaintLocation(Graphics g, Point size, Point p) {
		g.setColor(Color.black);
		if (p.x == 0 && p.y == 0) {
			g.drawLine(size.x / 2, size.y / 2, size.x, size.y / 2);
			g.drawLine(size.x / 2, size.y / 2, size.x / 2, size.y);
		} else if (p.x == 0 && p.y == 12) {
			g.drawLine(size.x / 2, size.y / 2, size.x, size.y / 2);
			g.drawLine(size.x / 2, size.y / 2, size.x / 2, 0);
		} else if (p.x == 12 && p.y == 0) {
			g.drawLine(size.x / 2, size.y / 2, 0, size.y / 2);
			g.drawLine(size.x / 2, size.y / 2, size.x / 2, size.y);
		} else if (p.x == 12 && p.y == 12) {
			g.drawLine(size.x / 2, size.y / 2, 0, size.y / 2);
			g.drawLine(size.x / 2, size.y / 2, size.x / 2, 0);
		} else if (p.x == 0) {
			g.drawLine(size.x / 2, 0, size.x / 2, size.y);
			g.drawLine(size.x / 2, size.y / 2, size.x, size.y / 2);
		} else if (p.x == 12) {
			g.drawLine(size.x / 2, 0, size.x / 2, size.y);
			g.drawLine(size.x / 2, size.y / 2, 0, size.y / 2);
		} else if (p.y == 0) {
			g.drawLine(0, size.y / 2, size.x, size.y / 2);
			g.drawLine(size.x / 2, size.y / 2, size.x / 2, size.y);
		} else if (p.y == 12) {
			g.drawLine(0, size.y / 2, size.x, size.y / 2);
			g.drawLine(size.x / 2, size.y / 2, size.x / 2, 0);
		} else {
			g.drawLine(0, size.y / 2, size.x, size.y / 2);
			g.drawLine(size.x / 2, 0, size.x / 2, size.y);
		}
		if (chessMap.containsKey(p)) {
			if (chessMap.get(p) == 0) {
				g.setColor(Color.black);
				g.fillOval(size.x / 4, size.y / 4, size.x / 2, size.y / 2);
			} else {
				g.setColor(Color.white);
				g.fillOval(size.x / 4, size.y / 4, size.x / 2, size.y / 2);
			}
		}
	}

	@Override
	public boolean IsActionableWithoutTarget(Point source) {
		return true;
	}

	@Override
	public void Action(Point source) {
		if (!chessMap.containsKey(source)) {
			chessMap.put(source, GetCurrentPlayer());
			int row = 0;
			int column = 0;
			int toWin = 0;

			// ���׽u
			row = source.x;
			column = source.y;
			while (true) {
				if ((row == 0) || (column == 0)) {
					break;
				} else {
					row--;
					column--;
				}
			}
			while (true) {
				if ((row == 12) || (column == 12) || (toWin >= 5))
					break;
				if (chessMap.get(new Point(row, column)) != null) {
					if (chessMap.get(new Point(row, column)) == GetCurrentPlayer())
						toWin++;
					else
						toWin = 0;
				} else
					toWin = 0;
				row++;
				column++;
			}
			if (toWin >= 5) {
				listener.OnGameOver();
				return;
			} else
				toWin = 0;

			// �k�׽u
			row = source.x;
			column = source.y;
			while (true) {
				if ((row == 12) || (column == 0)) {
					break;
				} else {
					row++;
					column--;
				}
			}

			while (true) {
				if ((row == 0) || (column == 12) || (toWin >= 5))
					break;
				if (chessMap.get(new Point(row, column)) != null) {
					if (chessMap.get(new Point(row, column)) == GetCurrentPlayer())
						toWin++;
					else
						toWin = 0;
				} else
					toWin = 0;
				row--;
				column++;
			}
			if (toWin >= 5) {
				listener.OnGameOver();
				return;
			} else
				toWin = 0;

			// �����u
			row = 0;
			column = source.y;
			while (true) {
				if ((row == 12) || (toWin >= 5))
					break;
				if (chessMap.get(new Point(row, column)) != null) {
					if (chessMap.get(new Point(row, column)) == GetCurrentPlayer())
						toWin++;
					else
						toWin = 0;
				} else
					toWin = 0;
				row++;
			}
			if (toWin >= 5) {
				listener.OnGameOver();
				return;
			} else
				toWin = 0;

			// �����u
			row = source.x;
			column = 0;
			while (true) {
				if ((column == 12) || (toWin >= 5))
					break;
				if (chessMap.get(new Point(row, column)) != null) {
					if (chessMap.get(new Point(row, column)) == GetCurrentPlayer())
						toWin++;
					else
						toWin = 0;
				} else
					toWin = 0;
				column++;
			}
			if (toWin >= 5) {
				listener.OnGameOver();
				return;
			} else
				toWin = 0;
			NextPlayer();
		}
	}

	@Override
	public void Action(Point source, Point target) {
	}

	@Override
	public Set<Point> GetValidTargets(Point source) {
		return new HashSet<Point>();
	}

	public FiveChess() {
		super();
	}
}
